<?php

namespace app\admin\controller;

use other\Uploader;

/**
 * 公共控制器类
 * @package app\admin\controller
 */
class Common extends Controllers
{
    /**
     * 单文件上传
     *
     * @return \think\Response
     */
    public function postUploader( Uploader $uploader )
    {
        $file = $this->request->file('file');
        $config = config('files.');
        $action = 'upload_'.$config['type'];
        $file_path = $uploader->$action( $file );
        if ( $file_path ) {
            return success(['path'=>$file_path]);
        }
        return failed('上传失败');
    }

    /**
     * 多文件上传(目前还未用到,先这么写着)
     *
     * @return \think\Response
     */
    public function postMultipleUploader( Uploader $uploader )
    {
        $files = $this->request->file('file');
        $config = config('files.');
        $action = 'upload_'.$config['type'];
        $file_path_data = [];
        foreach ( $files as $_file ) {
            $file_path = $uploader->$action( $_file );
            if ( $file_path ) {
                $file_path_data[] = $file_path;
            }
        }
        return success(['file_path_data'=>$file_path_data]);
    }

    /**
     * 音频上传
     *
     * @return \think\Response
     */
    public function postAudio( Uploader $uploader )
    {
        $file = $this->request->file('file');
        $config = config('files.');
        $action = 'upload_'.$config['type'];
        $file_path = $uploader->$action( $file, 'audio' );
        if ( $file_path ) {
            return success(['path'=>$file_path]);
        }
        return failed('上传失败');
    }

    /**
     * 七牛云获取token
     *
     * @return \think\Response
     */
    public function qiniuToken()
    {
        return json(['uptoken'=>app('uploader_service')->uploadToken()]);
    }

}
